﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.AnonymousMethod"/>.
    /// </summary>
    public class AnonymousMethodVM : ExpressionVM, IBlockVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(AnonymousMethod),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new AnonymousMethodVM((AnonymousMethod)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<ParameterDeclVM> _parameterVMs;
        protected BlockVM _bodyVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.AnonymousMethod"/>.
        /// </summary>
        public AnonymousMethodVM(AnonymousMethod anonymousMethod, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(anonymousMethod, dictionary)
        {
            _parameterVMs = CreateListVM<ParameterDecl, ParameterDeclVM>(anonymousMethod.Parameters, dictionary);
            _bodyVM = new BlockVM(anonymousMethod.Body, this, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.AnonymousMethod"/> model.
        /// </summary>
        public AnonymousMethod AnonymousMethod
        {
            get { return (AnonymousMethod)CodeObject; }
        }

        /// <summary>
        /// The <see cref="BlockVM"/> body.
        /// </summary>
        public BlockVM BodyVM
        {
            get { return _bodyVM; }
        }

        /// <summary>
        /// The parameters of the anonymous method (if any).
        /// </summary>
        public ChildListVM<ParameterDeclVM> Parameters
        {
            get { return _parameterVMs; }
        }

        /// <summary>
        /// True if the anonymous method has any parameters.
        /// </summary>
        public bool HasParameters
        {
            get { return (_parameterVMs != null && _parameterVMs.Count > 0); }
        }

        /// <summary>
        /// The number of parameters the anonymous method has.
        /// </summary>
        public int ParameterCount
        {
            get { return (_parameterVMs != null ? _parameterVMs.Count : 0); }
        }

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.Wheat; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.AntiqueWhite; }
        }

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            if (!flags.HasFlag(RenderFlags.Description))
            {
                base.RenderAfter(renderer, flags);
                if (_bodyVM != null)
                    _bodyVM.Render(renderer, flags);
            }
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            int newLines = AnonymousMethod.NewLines;
            if (newLines > 0)
            {
                if (!flags.HasFlag(RenderFlags.SuppressNewLine))
                    renderer.NewLines(newLines, ParentVM);
            }
            else if (flags.HasFlag(RenderFlags.PrefixSpace))
                renderer.RenderText(" ", PUNC_BRUSH, ParentVM);

            bool increaseIndent = (flags.HasFlag(RenderFlags.IncreaseIndent) && !flags.HasFlag(RenderFlags.NoIncreaseIndent));
            bool hasBorder = (flags.HasFlag(RenderFlags.ForceBorder) || HasBorder()) && !flags.HasFlag(RenderFlags.NoBorder);
            if (hasBorder)
            {
                CreateBorder(renderer, flags);
                increaseIndent = true;  // Always increase the indent level for wrapped lines inside a border
            }

            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderBefore(renderer, passFlags | RenderFlags.IsPrefix);

            // Increase the indent level for any newlines that occur within the expression if the flag is set
            if (increaseIndent)
                renderer.IndentOnNewLineBegin(this);

            bool hasParens = AnonymousMethod.HasParens;
            if (hasParens)
                renderer.RenderText(Expression.ParseTokenStartGroup, PUNC_BRUSH, this);
            RenderExpression(renderer, passFlags | (flags & (RenderFlags.Attribute | RenderFlags.HasDotPrefix | RenderFlags.Declaration)));
            if (AnonymousMethod.HasTerminator && !StatementVM.HideTerminators && !flags.HasFlag(RenderFlags.Description))
                renderer.RenderText(Statement.ParseTokenTerminator, PUNC_BRUSH, this);
            if (!flags.HasFlag(RenderFlags.NoEOLComments))
                RenderEOLComments(renderer, flags);

            RenderAfter(renderer, passFlags | (flags & RenderFlags.NoPostAnnotations));
            if (hasParens)
            {
                if (AnonymousMethod.IsEndFirstOnLine)
                    renderer.NewLine();
                renderer.RenderText(Expression.ParseTokenEndGroup, PUNC_BRUSH, this);
            }

            if (increaseIndent)
                renderer.IndentOnNewLineEnd(this);

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(this);
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (!flags.HasFlag(RenderFlags.Description))
            {
                renderer.RenderText(AnonymousMethod.ParseToken, KEYWORD_BRUSH, this);
                bool hasArguments = (_parameterVMs != null || AnonymousMethod.HasInfixComments);
                bool showParens = (hasArguments && !MethodDeclBaseVM.HideMethodParens);
                renderer.RenderText(showParens ? AnonymousMethod.ParseTokenStart : (hasArguments ? " " : ""), PUNC_BRUSH, this);
                RenderInfixComments(renderer, 0, flags);
                renderer.RenderList(_parameterVMs, passFlags, this);
                if (showParens)
                {
                    if (AnonymousMethod.IsEndFirstOnLine)
                        renderer.NewLine();
                    renderer.RenderText(AnonymousMethod.ParseTokenEnd, PUNC_BRUSH, this);
                }
            }
            else
            {
                // If rendering as a description, just render as a delegate type
                CreateVM(AnonymousMethod.GetReturnType()).Render(renderer, passFlags | RenderFlags.IsPrefix);
                renderer.RenderText(AnonymousMethod.ParseToken, KEYWORD_BRUSH, this);
                renderer.RenderText(AnonymousMethod.ParseTokenStart, PUNC_BRUSH, this);
                renderer.RenderList(_parameterVMs, passFlags, this);
                if (AnonymousMethod.IsEndFirstOnLine)
                    renderer.NewLine();
                renderer.RenderText(AnonymousMethod.ParseTokenEnd, PUNC_BRUSH, this);
            }
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_parameterVMs, flags);
            if (_bodyVM != null)
                _bodyVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_parameterVMs);
            if (_bodyVM != null)
                _bodyVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
